<template>
    <div class="list-wrapper">
        <Block>
            <DataGrid :auto-load="autoLoad" ref="dataGridRef">
                <template #toolbar-search>
                    <div class="query-from-box">
                        <el-form>
                            <el-form-item>
                                <el-input v-model="queryData.messageKey" placeholder="集成者/标识符" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-select placeholder="消息类型" v-model="queryData.messageTypeNo" clearable>
                                    <el-option
                                        v-for="(item, i) in classifyList"
                                        :label="item.typeName"
                                        :key="i"
                                        :value="item.typeNo"
                                    />
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-select placeholder="集成范围" v-model="queryData.integrationScope" clearable>
                                    <el-option
                                        v-for="(value, key) in IntegrationScope"
                                        :label="value"
                                        :key="key"
                                        :value="key"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </div>
                </template>
                <template #toolbar-operation>
                    <el-button type="primary" @click="onCreateEdit()">新建</el-button>
                </template>
                <el-table-column type="index" label="序号" width="60" align="center" fixed />
                <el-table-column label="集成者" width="300" fixed show-overflow-tooltip>
                    <template #default="scope">
                        <el-button link type="primary" @click="onGoDetail(scope.row)">
                            {{ scope.row.integratorName }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="messageNo" label="标识符" show-overflow-tooltip />
                <el-table-column prop="typeName" label="消息类型" show-overflow-tooltip />
                <el-table-column label="集成范围">
                    <template #default="{ row }">
                        <div class="table-item-tag">
                            <el-tag :type="row.integrationScope === 'ALL' ? 'primary' : 'warning'">{{
                                IntegrationScope[row.integrationScope]
                            }}</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="135" fixed="right">
                    <template #default="scope">
                        <el-button link type="primary" @click="onCreateEdit(scope.row)"> 修改 </el-button>
                        <el-button link type="danger" @click="onDel(scope.row)"> 删除 </el-button>
                    </template>
                </el-table-column>
            </DataGrid>
        </Block>
    </div>
</template>
<script setup lang="ts">
import { messageIntegrationService, messageClassifyService } from '@great/services';
import { useRouter } from 'vue-router';
import { checkPermi } from '@/utils/ruoyi';
import { showPopupBox } from '@great/utils';
import { IntegrationScope } from '@/utils/constant';
import CreateEditModal from './components/list/CreateEditModal.vue';
import { DataGrid, Block, type DataGridInstance } from '@great/components';

const router = useRouter();
// 查询条件
const queryData = ref({
    messageKey: '',
    messageTypeNo: '',
    integrationScope: ''
});
const dataGridRef = useTemplateRef<DataGridInstance>('dataGridRef');
const autoLoad = reactive({
    debounce: true,
    reloadDeps: [queryData],
    onLoad: async (current, size) => {
        const res = await messageIntegrationService.getPage({
            current,
            size,
            ...queryData.value
        });
        return { data: res.data.records, total: res.data.total };
    }
});
const classifyList = ref<
    Array<{
        typeName: string;
        typeNo: string;
    }>
>([]);
// 查看详情
const onGoDetail = (row: { messageNo: string }) => {
    if (checkPermi(['message:integration:detail'])) {
        router.push({
            name: 'message-integration-detail',
            params: {
                id: row?.messageNo
            }
        });
    }
};
// 创建/编辑
const onCreateEdit = (row?: any) => {
    showPopupBox(CreateEditModal, {
        row: row,
        callBack: dataGridRef.value?.refresh
    });
};
// 删除
const onDel = async (row: { integratorName: string; messageNo: string }) => {
    ElMessageBox.confirm(`确认删除集成 ${row.integratorName} ？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            await messageIntegrationService.del(row.messageNo);
            ElNotification({
                title: '删除成功！',
                type: 'success'
            });
            dataGridRef.value?.refresh();
        })
        .catch(() => {});
};
const getClassify = async () => {
    const res: any = await messageClassifyService.getList({
        enable: true
    });
    classifyList.value = res.data;
};
getClassify();
</script>

<style scoped lang="scss">
.list-wrapper {
    .query-from-box {
        display: flex;
        align-items: center;
        .el-form {
            display: flex;
            .el-form-item {
                margin-bottom: 0;
                width: 192px;
            }
            .el-form-item + .el-form-item {
                margin-left: 10px;
            }
        }
    }
    :deep(.data-grid) {
        .table-item-tag {
            display: flex;
        }
    }
}
</style>
